<?php

namespace app\common\controller;

use think\Db;
use think\Exception;

/*
 * 短信类
 */

class Sms
{
    private static $max_num = 20;
    private static $conf = [
        'uid' => '',
        'password' => ''
    ];

    private static $type = [
        1 => '注册',
        2 => '设置二级密码',
        3 => '修改二级密码',
        4 => '修改登录密码',
        6 => '忘记密码',
        7 => '绑定微信',
        8 => '绑定支付宝',
        9 => '微信解绑',
        10 => '支付宝解绑',
        11 => '绑定银行卡',
        12 => '解绑银行卡',
    ];

    public static function reg_send( $phone, $type = 1 )
    {
        $num = rand( 100000, 999999 );
        $event_type = self::$type[ $type ];
        switch ( $type ) {
            case 1: //注册
                $msg = "【】您的注册验证码是{$num}如非本人操作，请忽略本短信。";
                break;
            case 2:
                //设置二级密码
                $msg = "【】您修改二级密码的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
            case 3:
                //修改二级密码验证
                $msg = "【】您修改二级密码的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
            case 4:
                //修改登录密码验证
                $msg = "【】您修改登录密码的验证码是{$num}如非本人操作，请忽略本短信。";
                break;
            case 6:
                //忘记密码
                $msg = "【】您忘记密码的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
            case 7:
                //绑定微信
                $msg = "【】您微信绑定的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
            case 8:
                //绑定支付宝
                $msg = "【】您支付宝绑定的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
            case 9: //微信解绑
                $msg = "【】您微信解除绑定的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
            case 10: //支付宝解绑
                $msg = "【】您支付宝解除绑定的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
            case 11:    //绑定银行卡
                $msg = "【】您银行卡绑定的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
            case 12:    //解绑银行卡
                $msg = "【】您银行卡更换的验证码是{$num}，如非本人操作，请忽略本短信。";
                break;
        }
        // 13,15 不需要限制发的时间和次数, 不需要验证
        if ( $type != 13 && $type != 15) {
            $time = [strtotime( date( 'Ymd' ) ), strtotime( date( 'Ymd' ) . '23:59:59' )];

            $sms = db( 'sms' )->where( ['mobile' => $phone, 'event' => $event_type, 'createtime' => ['in', $time]] )->count();

            $continue = db( 'sms' )->where( ['mobile' => $phone, 'event' => $event_type, 'times' => 0] )->order( 'id', 'DESC' )->value( 'createtime' );
            if ( $continue && $continue + 600 > time() ) {
                return ['status' => -2];
            }

            //超过每日类型下的短信条数
            if ( $sms >= self::$max_num ) {
                return ['status' => -1];
            }
        }

        Db::startTrans();
        $sms_insert = [
            'event' => $event_type,
            'mobile' => $phone,
            'code' => $num,
            'times' => 0,
            'status' => $type,
            'ip' => $_SERVER['REMOTE_ADDR'] ?? "未查到",
            'createtime' => time()
        ];
        $res = db( 'sms' )->insert( $sms_insert );
        if ( $res && !empty( $msg ) ) {

            $result = self::send( $phone, $msg );
            if ( $result ) {
                Db::commit();
                return ['status' => $result, 'code' => $num];
            }
            Db::rollback();
            return ['status' => -1];
        } else {
            Db::rollback();
            return ['status' => -1];
        }
    }

    public static function send( $phone, $msg )
    {
        $data = "username=%s&password=%s&mobile=%s&content=%s";
        $url = "http://120.55.248.18/smsSend.do?";
        $name = self::$conf['uid'];
        $pwd = md5( self::$conf['password'] );
        $pass = md5( $name . $pwd );
        $to = $phone;
        $content = $msg;
        $content = urlencode( $content );
        $rdata = sprintf( $data, $name, $pass, $to, $content );
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $rdata );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        $result = curl_exec( $ch );
        curl_close( $ch );
        return $result;
    }

    //验证注册验证码
    static function confirmation( $phone, $code = '', $type = 1 )
    {
        try {
            if ( $code == '111111' ) {
                return false;   //todo  测试时使用
            }
            if ( !$code ) {
                throw new Exception( '不存在验证码', -101 );
            }
            $time = time();
            $where = [
                'mobile' => $phone,
                'event' => self::$type[ $type ],
                'createtime' => ['>', $time - 600]
            ];
            $sms = db( 'sms' )->where( $where )->order( 'id desc' )->find();
            if ( !$sms ) {
                throw new Exception( '请重新获取验证码', -102 );
            }
            if ( $sms['times'] > 0 ) {
                throw new Exception( '已经使用', -103 );
            }
            if ( $sms['code'] != $code ) {
                throw new Exception( '验证码错误', -104 );
            }
            db( 'sms' )->where( ['id' => $sms['id']] )->setInc( 'times' );
        } catch ( Exception $e ) {
            return $e->getMessage();
        }
    }
}